package com.hxkcloud.patient.wxapi;

import android.content.Intent;
import android.os.Message;
import android.os.Bundle;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.hxkcloud.patient.R;
import com.hxkcloud.patient.activitys.BaseActivity;
import com.hxkcloud.patient.activitys.PayActivity;
import com.hxkcloud.patient.activitys.PaySuccessActivity;

public class WXPayEntryActivity extends BaseActivity implements IWXAPIEventHandler {
    // APP_ID 替换为你的应用从官方网站申请到的合法appId
    public static final String APP_ID = "wx9ce43769100c2cc1";
    private IWXAPI api;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wxpay_entry);

        api = WXAPIFactory.createWXAPI(this, APP_ID);
        api.handleIntent(getIntent(), this);

    }

    @Override
    protected void updateByHandler(Message msg) {

    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent,this);
    }
    // 微信发送请求到第三方应用时，会回调到该方法
    @Override
    public void onReq(BaseReq baseReq) {
        //Toast.makeText(this, "openid = " + baseReq.openId, Toast.LENGTH_SHORT).show();
        switch (baseReq.getType()) {
            case ConstantsAPI.COMMAND_GETMESSAGE_FROM_WX:
//                goToGetMsg();
              //  toastMsg("getMsg......");
                break;
            case ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX:
                // goToShowMsg((ShowMessageFromWX.Req) req);
            //    toastMsg("showMsg......");
                break;
            case ConstantsAPI.COMMAND_LAUNCH_BY_WX:
             //   Toast.makeText(this, "Launch From Weixin", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }

    }
    // 第三方应用发送到微信的请求处理后的响应结果，会回调到该方法
    @Override
    public void onResp(BaseResp baseResp) {
        if (baseResp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            String  code= String.valueOf(baseResp.errCode);
            if(code.equals("0")){
                code = "支付成功";
                Intent intent = new Intent(mContext, PaySuccessActivity.class);
                startActivity(intent);
                finish();
            }else{
                code = "支付失败";
                Intent payIntent = new Intent(mContext, PayActivity.class);
                payIntent.putExtra("codes","-1");
                startActivity(payIntent);
                finish();
            }

//            AlertDialog.Builder builder = new AlertDialog.Builder(this);
//            builder.setTitle("提示");
//            builder.setMessage(getString(R.string.pay_result_callback_msg, String.valueOf(baseResp.errCode)));
//            builder.show();

//            switch (baseResp.errCode) {
//            case BaseResp.ErrCode.ERR_OK:
//                //result = "发送成功";
//                Intent intent = new Intent(mContext, PaySuccessActivity.class);
//                startActivity(intent);
//                finish();
//                break;
//            case BaseResp.ErrCode.ERR_USER_CANCEL:
//              //  result = "发送取消";
//                Intent payIntent = new Intent(mContext, PayActivity.class);
//                payIntent.putExtra("codes","-1");
//                startActivity(payIntent);
//                finish();
//                break;
//            case BaseResp.ErrCode.ERR_AUTH_DENIED:
//             //   result = "发送被拒绝";
//
//                break;
//            default:
//             //   result = "发送返回";
//                break;
//        }
//            code = String.valueOf(baseResp.errCode);
//            if(code.equals("0")){
//                code = "支付成功";
//
//            }else{
//                code = "支付失败:"+baseResp.errCode;
//
//            }
//            if (code.equals("0")){
//                Intent intent = new Intent(mContext, PaySuccessActivity.class);
//                    startActivity(intent);
//                    finish();
//            }else {
//                Intent payIntent = new Intent(mContext, PayActivity.class);
//                payIntent.putExtra("codes","-1");
//                startActivity(payIntent);
//                finish();
//            }
//            android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this);
//            builder.setTitle("提示");
//            builder.setMessage(code );
//            builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {
//                @Override
//                public void onClick(DialogInterface dialog, int which) {
//                    dialog.dismiss();
//                   // if (code.equals("0")){
//                        Intent intent = new Intent(mContext, PaySuccessActivity.class);
//                        startActivity(intent);
//                        finish();
////                    }else {
////                        Intent payIntent = new Intent(mContext, PayActivity.class);
////                        payIntent.putExtra("codes","-1");
////                        startActivity(payIntent);
////                        finish();
////                    }
//                }
//            });
//            builder.setCancelable(false);
//            builder.show();
        }

     //   String result = "";
//
//

       // Toast.makeText(this, result, Toast.LENGTH_LONG).show();


    }
}
